package huxibianjie.com.lbscandroid.ui.new_activity;

import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

import huxibianjie.com.lbscandroid.R;
import huxibianjie.com.lbscandroid.base.BaseActivity;
import huxibianjie.com.lbscandroid.ui.new_fragment.AccountDetailsFragment;

/**
 * 账户明细
 */
public class AccountDetailsActivity extends BaseActivity implements View.OnClickListener {
    private TextView tvToTurn;
    private TextView tvWithdraw;

    private MyPagerAdapter mAdapter;
    private ViewPager viewPager;

    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private final String[] mTitles = {
            "1",//提币记录
            "2"//提现记录
    };

    @Override
    protected int getContentView() {
        return R.layout.activity_account_details;
    }

    @Override
    protected void initView() {
        initTitle(R.drawable.ic_back, "账户明细");
        tvToTurn = findViewById(R.id.tv_to_turn);
        tvWithdraw = findViewById(R.id.tv_withdraw);
        viewPager = findViewById(R.id.view_pager);
    }

    @Override
    protected void initData() {
        for (String title : mTitles) {
            mFragments.add(AccountDetailsFragment.getInstance(title));
        }
        mAdapter = new MyPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(mAdapter);
        tvToTurn.performClick();
    }

    @Override
    protected void initListener() {
        tvToTurn.setOnClickListener(this);
        tvWithdraw.setOnClickListener(this);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                clickTab(position == 0 ? tvToTurn : tvWithdraw);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void clickTab(View view) {
        Drawable dra = getResources().getDrawable(R.drawable.ic_account_details_line);
        dra.setBounds(0, 0, dra.getMinimumWidth(), dra.getMinimumHeight());
        switch (view.getId()) {
            case R.id.tv_to_turn:
                tvWithdraw.setCompoundDrawables(null, null, null, null);
                tvToTurn.setCompoundDrawables(null, null, null, dra);
                break;
            case R.id.tv_withdraw:
                tvToTurn.setCompoundDrawables(null, null, null, null);
                tvWithdraw.setCompoundDrawables(null, null, null, dra);
                break;
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_to_turn:
                viewPager.setCurrentItem(0);
                clickTab(view);
                break;
            case R.id.tv_withdraw:
                viewPager.setCurrentItem(1);
                clickTab(view);
                break;
        }
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
//            super.destroyItem(container, position, object);
        }
    }
}
